import { assetManager, _decorator, Component, AssetManager, Canvas, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property(Canvas) private canvas2d: Canvas = null;
    @property(Node) private toReleaseNode: Node = null;
    @property(Canvas) private playerCanvas: Canvas = null;

    private loadBundle(bundleName: string): Promise<AssetManager.Bundle> {
        return new Promise<AssetManager.Bundle>(rs => {
            assetManager.loadBundle(bundleName, (e, asset) => {
                if (e) {
                    console.error(e);
                    rs(null)
                    return;
                }
                rs(asset);
            });
        })
    }

    async start() {
        /** 加载全局脚本包 */
        await this.loadBundle("scriptAB");
        const game: any = this.node.addComponent("Game");
        await game.init({
            canvas2d: this.canvas2d,
            playerCanvas: this.playerCanvas,
            releaseBoostFun: () => {
                // 这里进行销毁首场景的渲染节点和释放资源等操作
                if (this.toReleaseNode === null) {
                    return;
                }
                this.toReleaseNode.destroy();
                this.toReleaseNode = null;
            }
        })
    }

}